// pages/orderDetails/orderDetails.js
const app = getApp()
var timeChange = require('../../utils/timeChange.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {

  
    bgImage: '/images/orderDetails_wave.png',
    stateIconImage: '/images/order_succeed.png',
    arrowImage: '/images/order_arrow.png',
    iconBgImage: '/images/order_more.png',
    soiId: '',
    soiModel: null,
    createTime: null,
    // 订单的描述
    finishT: null,
    // 订单状态的描述
    stateT: null,
    endTime: null,
    // 去支付倒计时时间
    modifyTime: null,
  },
  // 加载数据
  loadData(username, solid) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.baseNet + 'orderInfo/findOrderInfo?',
      data: {
        soiId: solid,
        username: username
      },
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        console.log(res.data)
        wx.hideLoading()
        if (res.data.messcode == '001') {
          var time = timeChange.formatTimeTwo(res.data.soi.createTime, 'Y-M-D h:m:s')
          that.setData({
            soiModel: res.data.soi,
            createTime: time,
            modifyTime: res.data.soi.modifyTime,
            endTime: res.data.soi.createTime
          })
          // that.countdown(that.data.endTime)
          that.judgeState(res.data.soi.state)
        } else if (res.data.messcode == '002') {
          // 没有数据
        } else {
          // 服务器异常
        }
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var phoneNumber = wx.getStorageSync('phoneNumber')
    console.log(options.soiId)
    this.setData({
      soiId: options.soiId,
    })
    this.loadData(phoneNumber, this.data.soiId)
  },

  judgeState(state) {
    switch (state) {
      case 0:
        // isLeftBtnHidden = true
        // isRightBtnHidden = false
        // leftTitle = ''
        // rightTitle = '取消订单'
        // leftTitleColor = ''
        // rightTitleColor = '#646464'
        // leftBorderColor = ''
        // rightBorderColor = '#969696'
        // btnState = 0
        break;
      case 1:
        // isLeftBtnHidden = true
        // isRightBtnHidden = false
        // leftTitle = '取消订单'
        // rightTitle = '去支付'
        // leftTitleColor = ''
        // rightTitleColor = '#01E2E6'
        // leftBorderColor = ''
        // rightBorderColor = '#01E2E6'
        // btnState = 1
        this.countdown(this.data.modifyTime)
        break;
      case 2:
        // isLeftBtnHidden = true
        // isRightBtnHidden = false
        // leftTitle = ''
        // rightTitle = '申请退款'
        // leftTitleColor = ''
        // rightTitleColor = '#01E2E6'
        // leftBorderColor = ''
        // rightBorderColor = '#01E2E6'
        // btnState = 2
        break;
      case 3:
        // isLeftBtnHidden = true
        // isRightBtnHidden = false
        // leftTitle = ''
        // rightTitle = '申请退款'
        // leftTitleColor = ''
        // rightTitleColor = '#01E2E6'
        // leftBorderColor = ''
        // rightBorderColor = '#01E2E6'
        // btnState = 2
        break;
      case 4:
        // isLeftBtnHidden = true
        // isRightBtnHidden = false
        // leftTitle = ''
        // rightTitle = '去评价'
        // leftTitleColor = ''
        // rightTitleColor = '#01E2E6'
        // leftBorderColor = ''
        // rightBorderColor = '#01E2E6'
        // btnState = 3
        break;
      case 5:
        // isLeftBtnHidden = true
        // isRightBtnHidden = false
        // leftTitle = ''
        // rightTitle = '已评价'
        // leftTitleColor = ''
        // rightTitleColor = '#cccccc'
        // leftBorderColor = ''
        // rightBorderColor = '#cccccc'
        break;
      case 6:
        // isLeftBtnHidden = true
        // isRightBtnHidden = false
        // leftTitle = ''
        // rightTitle = '已取消'
        // leftTitleColor = ''
        // rightTitleColor = '#cccccc'
        // leftBorderColor = ''
        // rightBorderColor = '#cccccc'
        break;
      case 7:
        // isLeftBtnHidden = true
        // isRightBtnHidden = false
        // leftTitle = ''
        // rightTitle = '退款成功'
        // leftTitleColor = ''
        // rightTitleColor = '#cccccc'
        // leftBorderColor = ''
        // rightBorderColor = '#cccccc'
        break;
      case 8:
        // isLeftBtnHidden = true
        // isRightBtnHidden = false
        // leftTitle = ''
        // rightTitle = '查看退款详情'
        // leftTitleColor = ''
        // rightTitleColor = '#646464'
        // leftBorderColor = ''
        // rightBorderColor = '#969696'
        // btnState = 4
        break;
      case 9:
        // isLeftBtnHidden = true
        // isRightBtnHidden = false
        // leftTitle = ''
        // rightTitle = '查看退款详情'
        // leftTitleColor = ''
        // rightTitleColor = '#646464'
        // leftBorderColor = ''
        // rightBorderColor = '#969696'
        // btnState = 4
        break;
      case 10:
        // isLeftBtnHidden = true
        // isRightBtnHidden = false
        // leftTitle = ''
        // rightTitle = '已失效'
        // leftTitleColor = ''
        // rightTitleColor = '#cccccc'
        // leftBorderColor = ''
        // rightBorderColor = '#cccccc'
        break;
      case 11:
        // isLeftBtnHidden = true
        // isRightBtnHidden = false
        // leftTitle = ''
        // rightTitle = '退款成功'
        // leftTitleColor = ''
        // rightTitleColor = '#cccccc'
        // leftBorderColor = ''
        // rightBorderColor = '#cccccc'
        break;
      case 12:
        // isLeftBtnHidden = true
        // isRightBtnHidden = false
        // leftTitle = ''
        // rightTitle = '已失效'
        // leftTitleColor = ''
        // rightTitleColor = '#cccccc'
        // leftBorderColor = ''
        // rightBorderColor = '#cccccc'
        break;
      case 13:
        // isLeftBtnHidden = true
        // isRightBtnHidden = false
        // leftTitle = ''
        // rightTitle = '申请退款'
        // leftTitleColor = ''
        // rightTitleColor = '#ff6b6b'
        // leftBorderColor = ''
        // rightBorderColor = '#ff6b6b'
        // btnState = 2
        break;
    }
    this.setData({
    });
  },
  // 倒计时
  countdown(endTime) {
    var that = this;
    var EndTime = endTime || [];
    var NowTime = Date.parse(new Date()) / 1000;
    console.log(NowTime)
    console.log(endTime)
    var total_micro_second = EndTime + 60 * 30 - NowTime || [];
    console.log('剩余时间：' + total_micro_second);
    var cutdownT = that.dateformat(total_micro_second)
    console.log(cutdownT)
    // 渲染倒计时时钟
    that.setData({
      finishT: cutdownT
    });
    if(total_micro_second <= 0) {
      that.setData({
        finishT: "0:0:0"
      });
      return;
    }
  setTimeout(function () {
      total_micro_second -= 1;
      that.countdown(endTime);
    }
    , 1000)
 },
 
// 时间格式化输出，如11:03 25:19 每1s都会调用一次
  dateformat(micro_second) {
  // 总秒数
  var second = Math.floor(micro_second);
  // 天数
  var day = Math.floor(second / 3600 / 24);
  // 小时
  var hr = Math.floor(second / 3600 % 24);
  // 分钟
  var min = Math.floor(second / 60 % 60);
  // 秒
  var sec = Math.floor(second % 60);
  return hr + ":" +min + ":" + sec;
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.countdown()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})